package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/

import java.util.Vector;
import com.sound.ampache.objects.Artist;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectListField;

class ArtistsScreen extends ItemsScreen implements HTTPTransportListener {
    
	private Artist[] artists;
	private ArtistsListField artistsList;
	private AlbumsScreen albumsScreen;
	private SongsScreen songsScreen;
	public String screenTitle = "Artists";


		private MenuItem viewAlbumsMenu = new MenuItem("View Albums",100,10) {
			public void run() {
				viewAlbums();
			} 
		};
		
		private MenuItem viewSongsMenu = new MenuItem("View Songs",100,10) {
			public void run() {
				viewSongs();
			} 
		};
		 
		private MenuItem addSongsMenu = new MenuItem("Add Songs to Playlist",100,10) {
			public void run() {
				addSongs();
			} 
		};
    
	ArtistsScreen()
	{
		artistsList = new ArtistsListField();
		ItemsScreen.itemsList = artistsList;
        setTitle( new LabelField( screenTitle , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ) );
        artistsList.setEmptyString("No Artists", DrawStyle.HCENTER);
        this.add(artistsList); 
	}
	
	
	public void getArtists(){
		// this method is called to get all artists,
		// so get the cached artists if they exist
		// and if the user has it checked in Options
		// Also get artists if the user has checked
		// autoupdate and the artist catalog size has changed
		artists = DataStore.getCachedArtists();
		if(artists.length == 0 || artists == null ||
			DataStore.getCacheVal().booleanValue() == false ||
			(DataStore.getCacheVal().booleanValue() == true &&
					DataStore.getUpdateVal().booleanValue() == true &&
					artists.length != ampacheberry.comm.getArtists())) {
				getArtists("artists", "");
		} else {
			statusDlg.close();
			artistsList.set(artists);
		}
	}
	
	
	public void getArtists(String filter){
		getArtists("artists", filter);
	}
	
	
	public void getArtists(String method, String filter){
		setTitle(screenTitle);
		artistsList.set(null); 
		statusDlg.show();
		ampacheberry.comm.setHTTPTransportListener(this);
		ampacheberry.comm.send(method, filter); 
	}
	
	
    protected void makeMenu(Menu menu, int instance) {
        menu.add(viewAlbumsMenu);
        menu.add(viewSongsMenu);
        menu.add(addSongsMenu);
        menu.add(MenuItem.separator(addSongsMenu.getOrdinal() + 1));
        super.makeMenu(menu,instance);
    }
	
	
    private class ArtistsListField extends ObjectListField {
        
        private Bitmap icon = Bitmap.getBitmapResource("img/artist.png");  
        
        // We are going to take care of drawing the item.
        public void drawListRow(ListField listField, Graphics graphics, 
                int index, int y, int width) {     
                 
            int offsetY = (this.getRowHeight() - icon.getHeight())/2;
            graphics.drawBitmap(1,y + offsetY, icon.getWidth(),
                    icon.getHeight(),icon,0,0);
            graphics.drawText("Artist: " + artists[index].name, 
            		icon.getWidth() + 2, y + 1, DrawStyle.ELLIPSIS,
            		width - icon.getWidth() + 2);
            graphics.drawText("Albums: " + artists[index].albums, 
                    icon.getWidth() + 2, y + fontSize + 2,
                    DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            graphics.drawText("Songs: " + artists[index].songs, 
                    icon.getWidth() + 2, y + (2 * fontSize) + 2,
                    DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            graphics.drawLine(0, y + rowHeight - 1, listField.getWidth(),
            		y + rowHeight - 1);
        }
    };
	
    
	public void processResponse(final Vector data) {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run()
            {
            	statusDlg.close();
                artists = new Artist[data.size()];
    			data.copyInto(artists);
    			artistsList.set(artists);
    			
    			// cache the artists if the whole catalog was retrieved
    			// and the user has selected to cache the artists
    			if (data.size() == ampacheberry.comm.getArtists() &&
    				DataStore.getCacheVal().booleanValue() == true) {
	    				DataStore.setCachedArtists(artists);
    			}
            }
        });
	}

	
    protected boolean navigationClick(int status, int time)
    {
        viewAlbums();
        return true;
    }
    
	
    private void viewAlbums() {
    	int selected = artistsList.getSelectedIndex();
    	
    	if (null == albumsScreen) {
    		albumsScreen = new AlbumsScreen();
    	}
    	albumsScreen.screenTitle = "Albums by " + artists[selected].name;
    	albumsScreen.setRowHeight();
    	UiApplication.getUiApplication().pushScreen(albumsScreen);
        albumsScreen.getAlbums("artist_albums", artists[selected].id);
    }
    
    
    private void viewSongs() {
    	int selected = artistsList.getSelectedIndex();
    	
    	if (null == songsScreen) {
    		songsScreen = new SongsScreen();
    	}
    	songsScreen.screenTitle = "Songs by " + artists[selected].name;
    	songsScreen.setRowHeight();
    	UiApplication.getUiApplication().pushScreen(songsScreen);
        songsScreen.getSongs("artist_songs", artists[selected].id);
    }
    
    
    private void addSongs() {
    	int selected = artistsList.getSelectedIndex();
    	ampacheberry.playlistSong.addArtistToPlaylist(artists[selected]);
    }
    
    
} 
